package com.example.keshe0337.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.keshe0337.R;
import com.example.keshe0337.biz.UserService;
import com.example.keshe0337.entity.User;

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText etPhone = findViewById(R.id.et_phone);
        EditText etPassWord = findViewById(R.id.et_password);
        TextView tvRegister = findViewById(R.id.tv_register);

        Button btn = findViewById(R.id.btn_login);

        SharedPreferences prefs = this.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        btn.setOnClickListener(v -> {
            String phone = etPhone.getText().toString();
            String password = etPassWord.getText().toString();
            if (phone.isBlank()) {
                Toast.makeText(this,"请输入电话号码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.isBlank()) {
                Toast.makeText(this,"请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }

            UserService userService = new UserService(this);
            User user = new User();
            user.setPhone(phone);
            user.setPassword(password);
            Long login = userService.login(user);
            if (login >= 0L) {
                prefs.edit().putBoolean("manager", false).apply();
                Log.d("登陆","用户");
                prefs.edit().putLong("UID", login).apply();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();  // 关闭当前 Activity
            } else if (login.equals(-1L)) {
                Toast.makeText(this,"该手机未注册", Toast.LENGTH_SHORT).show();
            } else if (login.equals(-2L)) {
                Toast.makeText(this,"密码错误", Toast.LENGTH_SHORT).show();
            } else if (login.equals(-10L)) {
                prefs.edit().putBoolean("manager", true).apply();
                Log.d("登陆","管理员");
                Intent intent = new Intent(LoginActivity.this, ManageActivity.class);
                startActivity(intent);
                finish();  // 关闭当前 Activity
            }
        });

        tvRegister.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        TextView tvNotLogin = findViewById(R.id.tv_notLogin);
        tvNotLogin.setOnClickListener(v -> {
            prefs.edit().putBoolean("manager", false).apply();
            Log.d("登陆","用户");
            prefs.edit().putLong("UID", -1L).apply();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();  // 关闭当前 Activity
        });

    }
}
